﻿using System;
using KPIS.GERP.GIMS.MODEL;
using KPIS.DBM;
using System.Configuration;
using System.Collections.Generic;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_IcmMasDealerRepair
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_IcmMasDealerRepair> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_IcmMasDealerRepair> infoList = new List<M_IcmMasDealerRepair>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_dealer_repair_seq,icm_dealer_repair_name,icm_dealer_address "
                        +" ,icm_remark "
                        +" FROM icm_mas_dealer_repair "
                        +" WHERE record_status <>'D'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmMasDealerRepair info = new M_IcmMasDealerRepair();
                        info.icm_dealer_repair_seq = Convert.ToInt32(drc[i]["icm_dealer_repair_seq"]);
                        info.icm_dealer_repair_name = drc[i]["icm_dealer_repair_name"].ToString();
                        info.icm_dealer_address = drc[i]["icm_dealer_address"].ToString();
                        info.icm_remark = drc[i]["icm_remark"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public M_IcmMasDealerRepair GetListFillBySeq(Nullable<Int32> icm_dealer_repair_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_IcmMasDealerRepair info = new M_IcmMasDealerRepair();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_dealer_repair_seq", icm_dealer_repair_seq);

                strSQL = " SELECT icm_dealer_repair_seq,icm_dealer_repair_name,icm_dealer_address "
                        +" ,icm_remark "
                        +" FROM icm_mas_dealer_repair "
                        + " WHERE record_status <>'D' and icm_dealer_repair_seq = @icm_dealer_repair_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_dealer_repair_seq = Convert.ToInt32(drc[0]["icm_dealer_repair_seq"]);
                    info.icm_dealer_repair_name = drc[0]["icm_dealer_repair_name"].ToString();
                    info.icm_dealer_address = drc[0]["icm_dealer_address"].ToString();
                    info.icm_remark = drc[0]["icm_remark"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public void Insert_Dealer_Repair(M_IcmMasDealerRepair info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);
                dbManager.AddParameters(0, "@icm_dealer_repair_name", info.icm_dealer_repair_name);
                dbManager.AddParameters(1, "@icm_dealer_address", info.icm_dealer_address);
                dbManager.AddParameters(2, "@icm_remark", info.icm_remark);
                dbManager.AddParameters(3, "@record_status", info.record_status);
                dbManager.AddParameters(4, "@created_by", info.created_by);
                dbManager.AddParameters(5, "@created_when", info.created_when);

                strSQL = " INSERT  INTO icm_mas_dealer_repair "
                        +" (icm_dealer_repair_name,icm_dealer_address,icm_remark,record_status,created_by,created_when) "
                        +" VALUES "
                        +" (@icm_dealer_repair_name,@icm_dealer_address,@icm_remark,@record_status,@created_by,@created_when) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update_Dealer_Repair(M_IcmMasDealerRepair info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(7);
                dbManager.AddParameters(0, "@icm_dealer_repair_seq", info.icm_dealer_repair_seq);
                dbManager.AddParameters(1, "@icm_dealer_repair_name", info.icm_dealer_repair_name);
                dbManager.AddParameters(2, "@icm_dealer_address", info.icm_dealer_address);
                dbManager.AddParameters(3, "@icm_remark", info.icm_remark);
                dbManager.AddParameters(4, "@record_status", info.record_status);
                dbManager.AddParameters(5, "@updated_by", info.updated_by);
                dbManager.AddParameters(6, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_mas_dealer_repair "
                        + " SET icm_dealer_repair_name = @icm_dealer_repair_name "
                        + " ,icm_dealer_address = @icm_dealer_address "
                        + " ,icm_remark = @icm_remark "
                        + " ,record_status = @record_status "
                        + " ,updated_by =@updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE icm_dealer_repair_seq = @icm_dealer_repair_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Dealer_Dealer_Repair(M_IcmMasDealerRepair info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@icm_dealer_repair_seq", info.icm_dealer_repair_seq);
                dbManager.AddParameters(1, "@updated_by", info.updated_by);
                dbManager.AddParameters(2, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_mas_dealer_repair "
                        + " SET record_status = 'D' "
                        + " ,updated_by =@updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE icm_dealer_repair_seq = @icm_dealer_repair_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

    }
}
